package org.lhh.server.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.apache.commons.lang3.StringUtils;
import org.lhh.server.config.web.exception.ErrorException;

import java.io.IOException;
import java.util.List;
import java.util.Map;


public class JsonUtils {

    private static final ObjectMapper mObjectMapper = new ObjectMapper();
    private static final ObjectMapper mObjectMapperLowerCaseWithUnderScores = new ObjectMapper();

    static {

        mObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        mObjectMapperLowerCaseWithUnderScores.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        mObjectMapperLowerCaseWithUnderScores.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy());
    }

    public static String beanToJson(Object object) {
        if (object != null) {
            try {
                return mObjectMapper.writeValueAsString(object);
            } catch (JsonProcessingException e) {
                throw new ErrorException("系统异常：对象转JSON字符串异常", e);
            }
        }
        return "";
    }


    public static <T> T jsonToBean(String json, Class<T> classType) {
        if (StringUtils.isNotBlank(json) && classType != null) {
            try {
                return mObjectMapper.readValue(json, classType);
            } catch (IOException e) {
                throw new ErrorException("系统异常：对象转JSON字符串异常", e);
            }
        }
        return null;
    }

    public static <T> List<T> jsonToList(String json, Class<T> classType) {
        if (StringUtils.isNotBlank(json) && classType != null) {
            try {
                return mObjectMapper.readValue(json, mObjectMapper.getTypeFactory().constructCollectionType(List.class, classType));
            } catch (IOException e) {
                throw new ErrorException("系统异常：JSON转List数组异常", e);
            }
        }
        return null;
    }

    public static <k, v> Map<k, v> jsonToMap(String json, Class<k> kType, Class<v> vType) {
        if (StringUtils.isNotBlank(json)) {
            try {
                return mObjectMapper.readValue(json, mObjectMapper.getTypeFactory().constructMapType(Map.class, kType, vType));
            } catch (IOException e) {
                throw new ErrorException("系统异常：JSON转Map异常", e);
            }
        }
        return null;
    }

    public static String beanToJsonByLowerCase(Object object) {
        if (object != null) {
            try {
                return mObjectMapperLowerCaseWithUnderScores.writeValueAsString(object);
            } catch (JsonProcessingException e) {
                throw new ErrorException("系统异常：对象转JSON（忽略大小写）字符串异常", e);
            }
        }
        return "";
    }

    public static <T> T jsonToBeanByLowerCase(String json, Class<T> classType) {
        if (StringUtils.isNotBlank(json)) {
            try {
                return mObjectMapperLowerCaseWithUnderScores.readValue(json, classType);
            } catch (IOException e) {
                throw new ErrorException("系统异常：JSON字符串转对象（忽略大小写）异常", e);
            }
        }
        return null;
    }

}
